<?php
// $Id$

/**
 * @file
 * TODO
 */

function _portals_get_layout() {
  $args = func_get_args();
  if (isset($args[0])) {
    return 'Welcome to the most confusing form of your life'. drupal_get_form('_portals_full_layout_form', $args[0]);
  }
}

function _portals_full_layout_form($form_state, $portal_name){
  $items = _portals_form_load_defaults_from_portal_name($portal_name);
  if ($items == -1) {
    drupal_set_message("No portal was found with the name $portal_name.");
    drupal_goto('admin/settings/portals');
  }
  else {
    return array_merge(
      _portals_main_form($portal_name, $items),
      _portals_submit_form()
    );
  }
}

function _portals_main_form ($portal_name, $items = array()) {
  //dpr ($items);
  $rows = array();
  $blank = array();
  foreach($items as &$item) {
    if (isset($item->layout)) {
      $rows[$item->layout['row']][$item->layout['col']][$item->layout['pri']][] = $item;
    }
    else {
      $blank[] = $item;
    }
  }

  if (!empty($rows)) {
    foreach($rows as $row => &$cols) {
      foreach($cols as $col => &$pris) {
        ksort($pris);
      }
      ksort($cols);
    }
    ksort($rows);
  }
  
  
  
  $form['portal_name'] = array(
    '#type' => 'value',
    '#value' => $portal_name,
  );
  
  //dpr($rows);
  if (!empty($rows)) {
    $form['filled'] = array(
      '#type' => 'fieldset',
      '#title' => 'Layout',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );
    foreach($rows as $row => &$cols) {
      $form['filled'][$row] = array(
        '#type' => 'fieldset',
        '#title' => "Row $row",
      );
      foreach($cols as $col => &$pris) {
        $form['filled'][$row][$col] = array(
          '#type' => 'fieldset',
          '#title' => "Col $col",
        );
        foreach($pris as $pri => &$objects) {
          $form['filled'][$row][$col][$pri] = array(
            '#type' => 'fieldset',
            '#title' => "Pri $pri",
          );
          foreach($objects as $object_name => &$object) {
            $unique = _portals_get_item_unique($object);
            $form['filled'][$row][$col][$pri][$unique] = array(
              '#type' => 'fieldset',
              '#title' => "Item {$unique}",
            );
            $form['filled'][$row][$col][$pri][$unique]['item'] = array(
              '#type' => 'value',
              '#value' => $object,
            );
            $form['filled'][$row][$col][$pri][$unique]['layout']['row'] = array(
              '#type' => 'select',
              '#title' => 'Row',
              '#default_value' => $object->layout['row'],
              '#options' => array(0,1,2,3,4,5,6,7,8,9),
              //'#description' => t('row'),
            );
            $form['filled'][$row][$col][$pri][$unique]['layout']['col'] = array(
              '#type' => 'select',
              '#title' => 'Col',
              '#default_value' => $object->layout['col'],
              '#options' => array(0,1,2,3,4,5,6,7,8,9),
              //'#description' => t('Col'),
            );
            $form['filled'][$row][$col][$pri][$unique]['layout']['pri'] = array(
              '#type' => 'select',
              '#title' => 'Pri',
              '#default_value' => $object->layout['pri'],
              '#options' => array(0,1,2,3,4,5,6,7,8,9),
              //'#description' => t('pri'),
            );
            //dpr($item);
          }
        }
      }
    }
  }
  
  if (!empty($blank)) {
    $form['blanks'] = array(
      '#type' => 'fieldset',
      '#title' => 'No Layout',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );
    foreach($blank as $item) {
      $unique = _portals_get_item_unique($item);
      $form['blanks'][$unique] = array(
        '#type' => 'fieldset',
        '#title' => $unique,
      );
      $form['blanks'][$unique]['item'] = array(
        '#type' => 'value',
        '#value' => $item,
      );
      $form['blanks'][$unique]['layout']['row'] = array(
        '#type' => 'select',
        '#title' => 'Row',
        '#default_value' => 0,
        '#options' => array(0,1,2,3,4,5,6,7,8,9),
        //'#description' => t('row'),
      );
      $form['blanks'][$unique]['layout']['col'] = array(
        '#type' => 'select',
        '#title' => 'Col',
        '#default_value' => 0,
        '#options' => array(0,1,2,3,4,5,6,7,8,9),
        //'#description' => t('Col'),
      );
      $form['blanks'][$unique]['layout']['pri'] = array(
        '#type' => 'select',
        '#title' => 'Pri',
        '#default_value' => 0,
        '#options' => array(0,1,2,3,4,5,6,7,8,9),
        //'#description' => t('pri'),
      );
    }
  }
  return $form;
}
function _portals_submit_form ($handler = "_portals_process_layout_form") {
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array("$handler"),
  );
  return $form;
}

function _portals_process_layout_form ($form, &$form_state) {
  //dpr($form_state);
  
  $out_objects = array();
  if (isset($form_state['values']['blanks'])) {
    foreach ($form_state['values']['blanks'] as $module_name => $result_set) {
      $new_object = $result_set['item'];
      $new_object->layout = $result_set['layout'];
      $out_objects[] = $new_object;
    }
  }
  
  if (isset($form_state['values']['filled'])) {
    foreach($form_state['values']['filled'] as $row => &$cols) {
      //print ("The row is $row<br>");
      foreach($cols as $col => &$pris) {
        //print ("The col is $col<br>");
        foreach($pris as $pri => &$objects) {
          //print ("The pri is $pri<br>");
          foreach($objects as $object_name => &$object) {
            //print ("The object name is $object_name<br>");
            $new_object = $object['item'];
            $new_object->layout = $object['layout'];
            $out_objects[] = $new_object;
          }
        }
      }
    }
  }
  
  //dpr($out_objects);
  //die;

  $portal_name = $form_state['values']['portal_name'];
  $portal_settings = serialize($out_objects);
  $the_pid = _portals_node_portal_portalid_from_name($portal_name);
  if ($the_pid != -1) {
    _portals_save_cnf_to_pid($the_pid, $portal_settings);
    drupal_set_message("Portal $portal_name's layout updated.");
  }
  else {
    drupal_set_message("Portal $portal_name does not exist!.");
  }
  $form_state['redirect'] = 'admin/settings/portals';
}

function _portals_form_load_defaults_from_portal_name($portal_name) {

function _portals_get_item_unique($object) {
  if($object->type == 'page') {
    return 'page-' . $object->args[0];
  }
  elseif($object->type == 'block') {
    return 'block-' . $object->args[0] . $object->args[1];
  }

}
  portals_include('queries');
  $portal_id = _portals_node_portal_portalid_from_name($portal_name);
  $defaults = array();
  if ($portal_id != -1) {
    return _portals_get_cnf_from_pid($portal_id);
  } else
  {
    return -1;
  }
}